package org.javaboy.vhr.controller.order;

import com.github.pagehelper.PageInfo;
import org.javaboy.vhr.model.*;
import org.javaboy.vhr.service.OrderService;
import org.javaboy.vhr.utils.HrPOIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/getById")
    @ResponseBody
    public Orders getById(@RequestParam("id") int id){
        System.out.println(id+"123");
        return orderService.getById(id);
    }
    @GetMapping("/search")
    @ResponseBody
    public RespPageBean search(@RequestParam("keyword") String keyword){

        return orderService.search(1,10,keyword);
    }
    @GetMapping("/getPages")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return orderService.getEmployeeByPage(page, size);
    }
    @GetMapping("/getExcel")
    public ResponseEntity<byte[]> getEmployeeByPage() {
        List<Orders> orders = orderService.getList(1, 20);
        return HrPOIUtils.employee2Excel(orders);
    }

    @PostMapping("/save")
    public RespBean addEmp(@RequestBody Orders orders) {
        if (orderService.save(orders) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }
    @GetMapping("/delete")
    public RespBean delete(@RequestParam Integer id) {
        Integer result = orderService.delete(id);
        if ( result== 1) {
            return RespBean.ok("删除成功!");
        }
        if ( result== 2) {
            return RespBean.error("只允许管理员删除!");
        }
        return RespBean.error("删除失败!");
    }
    @PostMapping("/getTimes")
    public List<TimesRes> getTimes(@RequestBody Times times) {
        TimesRes timesRes = orderService.getTimes(times);
        List<TimesRes> list =new ArrayList<>();
        list.add(timesRes);
        return list;
    }
    @PostMapping("/getPersonInfo")
    public List<PersonResult> getPersonInfo(@RequestBody Times times) {
        List<PersonResult> personResultList = orderService.getPersonInfo(times);

        return personResultList;
    }
    @PostMapping("/update")
    public RespBean update(@RequestBody Orders orders) {

        System.out.println(orders.toString());
        Integer result = orderService.update(orders);
        if (result == 1) {
            return RespBean.ok("添加成功!");
        }
        if(result==5){
            return RespBean.error("该笔单子已经被处理，不允许编辑");
        }
        return RespBean.error("编辑失败!");
    }
    @GetMapping("/complete")
    public RespBean complete(@RequestParam Integer id) throws ParseException {
        int result = orderService.complete(id);
        if (result == 1) {
            return RespBean.ok("你真棒！");
        }if(result == 2){
           return RespBean.error("此操作只能该查单人进行操作!");
        }if(result==5){
           return  RespBean.error("这个单子已经处理过了！");
        }
        return RespBean.error("未知原因失败!");
    }

}
